using System;
using System.Collections.Generic;
using System.Text;
using Decav.Data;
using Decav.Gatsb.Data;
using Decav.Gatsb.Application;

namespace Decav.Gatsb
{
    /// <summary>
    /// A comment that is made on a <see cref="Post"/>.
    /// </summary>
    [Serializable]
    public class PostComment
    {
        protected PostComment(DataRecordAdapter adapt)
        {
            if (adapt == null)
                throw new ArgumentNullException("adapt");

            _createDate = adapt.GetDateTime("DatePosted");
            _owner = new UserLazyLoadReference(adapt.GetGuid("Owner"));
            _body = adapt.GetString("Body");
            _id = adapt.GetGuid("CommentId");
            _postId = adapt.GetGuid("PostId");
        }

        /// <summary>
        /// Creates a new <see cref="PostComment"/> for a post.
        /// </summary>
        /// <param name="postId">The unique identifier of the post to associate the comment with.</param>
        /// <param name="body">The body of the comment.</param>
        public PostComment(Guid postId, string body)
        {
            if (body == null)
                throw new ArgumentNullException("body");

            _createDate = DateTime.UtcNow;
            _owner = new UserLazyLoadReference(GatsbApplication.Current.CurrentUser);
            _body = body;
            _id = Guid.NewGuid();
            _postId = postId;
        }

        private DateTime _createDate;
        private UserLazyLoadReference _owner;
        private string _body;
        private Guid _id;
        private Guid _postId;

        /// <summary>
        /// Gets the unique identifier of the comment.
        /// </summary>
        public Guid Id
        {
            get
            {
                return _id;
            }
        }

        /// <summary>
        /// Gets the unique identifier of the post this comment is on.
        /// </summary>
        public Guid PostId
        {
            get
            {
                return _postId;
            }
        }

        /// <summary>
        /// Gets or sets the body of the post.
        /// </summary>
        public string Body
        {
            get
            {
                return _body;
            }
            set
            {
                _body = value;
            }
        }

        /// <summary>
        /// Gets the date that this post was created.
        /// </summary>
        public DateTime CreateDate
        {
            get
            {
                return _createDate;
            }
        }

        /// <summary>
        /// Gets the person who created this comment.
        /// </summary>
        public User Owner
        {
            get
            {
                return _owner.Value;
            }
        }
    }
}
